package com.lzx.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzx.shop.dao.PaymentRecordDao;
import com.lzx.shop.dataobject.PaymentRecordDO;
import com.lzx.shop.params.PaymentParam;
import com.lzx.shop.params.PaymentRecordQueryParam;
import com.lzx.shop.service.PaymentRecordService;
import com.lzx.shop.utils.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName PaymentRecordServiceImpl
 * @Author 刘正星
 * @Date 2021/3/27 上午11:05
 * @Description
 */
@Service
public class PaymentRecordServiceImpl implements PaymentRecordService {

    @Autowired
    private PaymentRecordDao paymentRecordDao;

    @Override
    public PaymentRecordDO save(PaymentRecordDO paymentRecord) {

        paymentRecord.setGmtModifed(LocalDateTime.now());
        if (StringUtils.isBlank(paymentRecord.getId())) {
            paymentRecord.setGmtCreated(LocalDateTime.now());
            paymentRecord.setId(UUIDUtil.getUUID());
            int result = paymentRecordDao.insert(paymentRecord);
            return result > 0 ? paymentRecord : null;
        }

        int update = paymentRecordDao.updateById(paymentRecord);
        return update > 0 ? paymentRecord : null;
    }

    @Override
    public List<PaymentRecordDO> query(PaymentRecordQueryParam queryParam) {
        QueryWrapper<PaymentRecordDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PaymentRecordDO::getUserId, queryParam.getUserId()).and(wq -> wq.eq(PaymentRecordDO::getOrderNumber, queryParam.getOrderNumber()));
        return paymentRecordDao.selectList(queryWrapper);
    }

    @Override
    public PaymentRecordDO updateStatus(PaymentRecordDO paymentRecord) {
        paymentRecord.setGmtModifed(LocalDateTime.now());

        return paymentRecordDao.updateById(paymentRecord) > 0 ? paymentRecord : null;
    }

    @Override
    public List<PaymentRecordDO> findAll() {
        return paymentRecordDao.selectList(null);
    }

    @Override
    public IPage<PaymentRecordDO> page(Page<PaymentRecordDO> page, PaymentParam param) {

        QueryWrapper<PaymentRecordDO> queryWrapper = new QueryWrapper<>();

        return paymentRecordDao.selectPage(page,queryWrapper);
    }
}
